from django.shortcuts import render, redirect
from django.contrib import messages
from .models import Contact

# Create your views here.
def contact(request):
    if request.method == 'POST':
        
        player_id = request.POST['player_id']
        player = request.POST['player']
        name = request.POST['name']
        email = request.POST['email']
        message = request.POST['message']
        user_id = request.POST['user_id']



        #check if user has made inquery already
        # if request.user.is_authenticated:
        #     user_id = request.user.id
        #     print('hello')
        #     has_contacted = Contact.objects.all().filter(player_id=player_id, user_id=user_id)
        #     if has_contacted:
        #         messages.error(request, 'You have already made an inquirt for this listing')
        #     return redirect('/players/'+player_id)
            

        contact = Contact(player_id=player_id, player=player,name=name,email=email,message=message,user_id=user_id)
        print('hello save')
        contact.save()

        messages.success(
            request, 'You request has been submitted, a realtor will get back to you soon')
        return redirect('/players/'+player_id)